import { LocalstorageDataManage } from './../localstoreage/localstoreageDataManage';
import axios from 'axios';

axios.defaults.timeout = 8000;

export interface HttpMethods {
    get(config?);
    post(body)
}

axios.interceptors.request.use((config) => {    // 这里的config包含每次请求的内容
    if (config.data) {
        config.data.user = LocalstorageDataManage.current.getUser();
        config.data.terminal = LocalstorageDataManage.current.getTerminal() ||  { "id": "mdelivery","name":"test" }
    }
    return config;
}, function (err) {
    return Promise.reject(err);
});

export function prefixHttp(prefix, config) {
    function ret(url) {
        return {
            get() {
                // !config && (config={});
                // config.timeout = 2500;
                return axios.get(prefix + url, config) 
            },
            post(body) { return axios.post(prefix + url, body, config) },
            put(body) { return axios.put(prefix + url, body, config) }
        }
    }
    ret['changePrefix'] = (newPrefix, newHeaders) => { prefix = newPrefix; config = newHeaders }
    return ret
}
